package indi.lqzhi.basic.ui.activity.splash;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ThreadUtils;

import java.util.List;
import java.util.concurrent.TimeUnit;

import indi.lqzhi.basic.R;
import indi.lqzhi.basic.tools.thirdparty.ThreadTaskTools;


public abstract class SplashActivity extends AppCompatActivity implements Runnable{

    @Override
    protected void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        TextView splash_text = findViewById(R.id.splash_text);
        ImageView splash_icon = findViewById(R.id.splash_icon);
        String text = splashText();
        if (text != null){
            splash_text.setText(text);
        }
        Drawable drawable = splashIcon();
        if (drawable != null){
            splash_icon.setImageDrawable(drawable);
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        ThreadUtils.executeBySingle(ThreadTaskTools.getTask(this));
    }

    @Override
    public void run() {
        long startTime = System.currentTimeMillis();
        try {
            initApp();
            Thread.sleep(waitTime());
        }catch (Exception exception){
            exception.printStackTrace();
            handlerInitException(exception);
        }
        //初始化完毕后，跳转
        runOnUiThread(() -> {
            LogUtils.d("跳转到主页。");
            ActivityUtils.startActivity(gotoActivity());
            long endTime = System.currentTimeMillis();
            LogUtils.d("初始化耗时："+(endTime - startTime) + "毫秒");
            finish();//结束
        });
    }

    /**
     * 必须重写的方法！！！！
     * @return 返回闪屏页面后所进行的事情
     */
    public abstract Class<? extends Activity> gotoActivity();

    /**
     * 此方法用于重新
     */
    public void initApp(){

    }

    /**
     * 此方法用于处理初始化异常
     */
    public void handlerInitException(Exception exception){

    }

    /**
     * 显示的文字
     * @return 文字
     */
    public String splashText(){
        return null;
    }

    /**
     * 图标
     * @return 显示的图标
     */
    public Drawable splashIcon(){
        return null;
    }

    /**
     * 等待延时
     * @return 默认为3000毫秒
     */
    public long waitTime(){
        return 3000;
    }
}
